<?php

namespace core\lib;

/**
 * 输出类
 */
class Response
{
    const ERROR_CODE = [
        '0' => '未知错误',
        '100' => '未知错误',
        '200' => '请求成功',
        '400' => '无效请求',
        '401' => '权限不足访问被拒绝',
        '402' => '用户名或密码错误',
        '403' => '禁止访问',
        '404' => '数据不存在',
        '500' => '内部服务器错误',
        '10000' => '请求参数错误',
        '10001' => '路由请求错误'
    ];

    public function create($data)
    {
        $code = intval($data['code']);
        if ($code !== 200) {
            if (APP_DEBUG) {
                // 开发阶段，给开发人员看的，最终还是要以生产为准
                header('Content-Type:application/json; charset=utf-8');
                $data['msg'] = self::ERROR_CODE[$data['code']];
                exit(@json_encode($data, JSON_UNESCAPED_UNICODE));
            }
            // 生产阶段
            exit(@json_encode($data, JSON_UNESCAPED_UNICODE));
        }
        $data['result'] = $data['result'] ?? '';

        $parse = parse_url($_SERVER['REQUEST_URI']);
        $whiteList = [
            '/', '/tool', '/tool/index',
            '/macencry', '/macencry/index',
            '/robots', '/robots/index',
            '/groupcheck', '/groupcheck/index',
            '/groupcheck3', '/groupcheck3/index',
            '/groupcheck4/step1', '/groupcheck4/step2','/groupcheck4/step11',
            '/spidercheck/step1', '/spidercheck/step2',
            '/domainlevel/step1', '/domainlevel/step2',
            '/robotsitemap', '/robotsitemap/index',
            '/groupstat', '/groupstat/index', '/groupstat/ips', '/groupstat/domains',
            '/base64/jiami', '/base64/jiemi',
            '/wptool', '/wptool/index', '/wptool/md5',
        ];
        if (isset($parse['path']) && in_array($parse['path'], $whiteList)) {
            // 工具页面，特殊处理
            exit($data['result']);
        } else {
            exit(@json_encode($data, JSON_UNESCAPED_UNICODE));
        }
    }
}
